package com.intellagric.controller.module.trace_record_table;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.TraceTable;
import com.intellagric.pojo.TraceTableRows;
import com.intellagric.service.traceability.trace_record_table.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/2/22
 * @DESC： 表格定义操作
 */
@Controller
public class TableController {

    @Autowired
    private TableService tableService;

    /**
     * 添加记录表格
     * @Param table
     * @return ResponseMessage
     */
    @RequestMapping("/table/add")
    @ResponseBody
    public ResponseMessage add(TraceTable table) {
        table.setId(table.getName());
        table.setCreateDate(new Date());
        table.setUpdateDate(new Date());
        for (TraceTableRows tableRows : table.getTableRows()) {
            tableRows.setId(UUID.randomUUID().toString().replaceAll("-",""));
            tableRows.setTableId(table.getName());
        }
        return tableService.addTable(table);
    }


    /**
     * 删除记录表格信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("/table/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return tableService.deleteTable(id);
    }

    /**
     * 修改记录表格信息
     * @Param TraceTable
     * @return ResponseMessage
     */
    @RequestMapping("/table/edit")
    @ResponseBody
    public ResponseMessage edit(TraceTable traceTable) {
        tableService.deleteTable(traceTable.getId());
        return tableService.addTable(traceTable);
    }

    /**
     * 查询记录表格信息
     * @Param
     * @return ResponseMessage
     */
    @RequestMapping("/table/{id}")
    @ResponseBody
    public TraceTable edit(@PathVariable("id") String id) {
        return tableService.getTableById(id);
    }

    /**
     * 查询记录表格信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/table/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return tableService.getTablesList();
    }

    /**
     * 分页查询记录表格信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/table/page")
    @ResponseBody
    public LayuiDataGridResult getPage(int page,int limit) {
        return tableService.getTablePage(page,limit);
    }

    /**
     * 分页查询记录表格信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/table/query")
    @ResponseBody
    public LayuiDataGridResult getPage(int page,int limit,TraceTable table) {
        return tableService.getTablePageByVo(page,limit,table);
    }

}
